<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Translation
{
	public static function add($translation)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Translation();
		$dao->setConnection($conn);
		return $dao->add($translation);
	}
	
	public static function delete($id, $class)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Translation();
		$dao->setConnection($conn);
		return $dao->delete($id, $class);
	}
	
	public static function update($translation)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Core_Models_Mysql_Translation();
		$dao->setConnection($conn);
		return $dao->update($translation);
	}
		
	public static function dbControllers($module = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$controllerDao = new Modules_Core_Models_Mysql_Controller();
		$controllerDao->setConnection($conn);
		return $controllerDao->dbControllers($module);
	}
	
	/**
	 * Get all translation items of given item
	 * 
	 * @param Gio_Core_Model $dao
	 * @param array $item
	 * @return array
	 */
	public static function translationItems($item, $daoInstance)
	{
		$configs = Gio_Core_Config_Xml::getConfig();
		if (!isset($configs->localization->languages->list) || $configs->localization->languages->list == $configs->web->language) {
			return array();
		}
		
		/**
		 * Create DAO instance based on the item class
		 */
		$conn        = Gio_Db_Connection::getConnection();
		$daoInstance->setConnection($conn);
		
		$result      = array();
		foreach (explode(',', $configs->localization->languages->list) as $lang) {
			$result[$lang] = null;
		}
		
		$translations = $daoInstance->getTranslations($item);
		if ($translations != null) {
			foreach ($translations as $translation) {
				$result[$translation['language']] = $translation;
			}
		}
		return $result;
	}
}